package com.foosun.util.wechat;

import java.io.IOException;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.foosun.context.EopSetting;
import com.foosun.util.https.HttpClientConnectionManager;

public class WxCommonUtils {
    // http客户端  
    public static DefaultHttpClient httpclient;  
    
    static {  
        httpclient = new DefaultHttpClient();  
        httpclient = (DefaultHttpClient) HttpClientConnectionManager.getSSLInstance(httpclient); // 接受任何证书的浏览器客户端  
    }  
  
    public static void main(String[] args) {  
        try {  
            // 获取accessToken -参数appid，secret  
            String accessToken = getAccessToken("wx9aa55a020a3241a5");  
            System.out.println(accessToken);  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
    }  
    
    /** 
     * 获取accessToken 
     */  
    public static String getAccessToken(String corpId) throws IOException {
        HttpGet get = HttpClientConnectionManager.getGetMethod("https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=" + corpId + "&corpsecret=" + EopSetting.CORP_SECRETS.get(corpId));  
        HttpResponse response = httpclient.execute(get);  
        String jsonStr = EntityUtils.toString(response.getEntity(), "utf-8");  
        JSONObject object = JSON.parseObject(jsonStr);
        String accessToken = object.getString("access_token");
        return accessToken;
    } 
}
